<?php
namespace Admin\Controller;
use Think\Controller;
class MasterController extends Controller {
    public function index(){

        // $mastersModel=M('masters');//构建一个操作masters表的模型实例
        // $masters=$mastersModel->where("m_states=1"/*必须是双引号*/)->select();
        // $masters=$mastersModel->where('m_states=1')->join('clubs  ON masters.cl_id=clubs.cl_id');
        // $mastersModel = M('masters')->order('m_id')->join('clubs  ON masters.cl_id=clubs.cl_id ')->select();
        // $this->index=$masters;
        // dump($masters);
        $mastersModel = M('masters');
        $qrcodenum = $mastersModel->where("m_states=1")->count();
        $Page = new \Think\Page($qrcodenum, 3);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
每页<b>3</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->link = $Page->show();
        $this->index = $mastersModel->where('m_states=1')->join('clubs  ON masters.cl_id=clubs.cl_id')->limit($Page->firstRow, 3)->select();
        // $this->index = $mastersModel->where("m_states=1")->order("m_id desc")->limit($Page->firstRow, 3)->select();
        $this->display();
       

    }
    public function delmaster($m_id){
    $mastersmodel=M('masters'); 
    //where实在构建条件
    if (false!==$mastersmodel->where("m_id=$m_id"/*必须是双引号*/)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
        
    }
    public function addMaster()
  {
    if(IS_POST)
      
    {
      $data['m_name'] = I('post.m_name');
      $password = I('post.m_password');
      $data['email'] = I('post.m_email');
      
      
      if(empty($data['m_name']))
      {
        $this->error('用户名必须填写');
      }
      elseif(empty($password))
      {
        $this->error('密码必须填写');
      }
      else
      {
        $data['m_password'] = md5(md5($password) );
        
        $master = M('masters');
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }
        if($master->create()){
            $master->m_pic1=$name; 
            if(false!==$master->add())
                      {
                $this->success('添加成功!','index');
            }
            else{$this->error('添加失败');}
        }else{
            $this->error($master->getError());
        }
      }
    }
    else
    { 
      $this->display();
    }    

  }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Upload/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        $upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }


    public  function editMaster($m_id){
		$masterInfos = M('masters');
		$master=$masterInfos->where("m_id=$m_id")->find();
		$this->masterInfos=$master;
		$this->display();
	}

	public function  doUpdate($m_id){
		$masterInfos = M('masters');
		//接收用户输入的内容
		$data['m_name'] = trim(I('post.m_intro'));
		$data['m_name'] = trim(I('post.m_name'));
		$data['m_address'] = trim(I('post.m_address'));


        if ($_FILES['file']['error'] == 0) {

                $info = $this->upload();
                $data['m_pic1'] = $info['file']['savename'];
            } else {
                $data['photo'] = "default.jpg";
            }


		$master=$masterInfos->where("m_id=$m_id")->save($data);

		if(false !== $master){
			$this->success('修改成功！','index');
		}else{
			$this->error('修改失败');
		}
	}
}
?>